<template>
  <el-dialog
    title="停止订单"
    :visible.sync="visible"
    width="60%"
    append-to-body
  >
    <avue-form
      ref="form"
      v-model="form"
      :option="pauseOrderFormOption"
    >
      <template #stopReason>
        <el-row>
          <el-radio
            v-model="form.stopReason"
            label="1"
          >
            猫咪去喵星
          </el-radio>
          <el-radio
            v-model="form.stopReason"
            label="2"
          >
            其他
          </el-radio>
          <el-input
            v-if="form.stopReason === '2'"
            v-model="stopReason"
            :span="12"
            style="width: 220px"
            placeholder="请详细描述停止原因"
          ></el-input>
        </el-row>
      </template>
      <template #adoptStopTime>
        <el-date-picker
          v-model="form.adoptStopTime"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        ></el-date-picker>
        <div style="color: red; font-size: 12px">
          温馨提示：因涉及订单退款，请跟进真实情况选择该用户实际云认养结束日期！
        </div>
      </template>
    </avue-form>
    <template #footer>
      <el-button
        type="primary"
        :loading="loading"
        @click="save"
      >
        确定无误并停止订单
      </el-button>
      <el-button @click="visible = false">取消</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { pauseOrderFormOption } from '@/const/crud/pet/cloudAdoptOrder.js';
import { manualStopOrder } from '@/api/core/adopt';

export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    orderCode: {
      type: String,
    },
    startTime: {
      type: String,
      default: '',
    },
    endTime: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      pauseOrderFormOption,
      form: {
        stopReason: '1',
      },
      stopReason: '',
      loading: false,
      pickerOptions: {
        // disabledDate(time) {
        //   return time.getTime() >new Date(this.startTime).getTime()
        // },
      },
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      },
    },
  },
  watch: {
    visible(val) {
      if (!val) {
        this.$refs.form.resetFields();
      } else {
        const startDate = new Date(this.startTime);
        const endDate = new Date(this.endTime);
        this.pickerOptions = {
          disabledDate(time) {
            return (
              time.getTime() < startDate.getTime() - 24 * 60 * 60 * 1000 ||
              time.getTime() > endDate.getTime()
            );
          },
        };
      }
    },
  },
  methods: {
    save() {
      this.$refs.form.validate((valid, done) => {
        if (this.form.stopReason === '2') {
          if (!this.stopReason) {
            this.$message.error('请输入原因');
            return;
          }
        }
        if (valid) {
          try {
            this.loading = true;
            const time = `${this.form.adoptStopTime} 23:59:59`;
            const stopReason =
              this.form.stopReason === '1' ? '猫咪去喵星' : this.stopReason;
            const data = {
              refundReason: this.form.refundReason,
              stopTime: time,
              stopReason,
              orderCode: this.orderCode,
              refundType: this.form.refundType,
            };
            manualStopOrder(data)
              .then((res) => {
                this.$emit('refresh');
                this.visible = false;
                this.loading = false;
                this.$message.success('停止成功！');
                done();
              })
              .catch((err) => {
                done();
                this.loading = false;
                throw new Error(err);
              });
          } catch (error) {
            this.loading = false;
            done();
            throw new Error(error);
          }
        }
      });
    },
  },
};
</script>
